package net.lahuobao.export.datasoruce.entity;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;

import javax.persistence.Entity;

import net.lahuobao.export.datasoruce.annotation.ExportField;
@Entity
@SuppressWarnings("serial")
public class Paybill {
	@ExportField(title="主键",isShow=0)
	private String id ;
	@ExportField(title="货物id",sort=10)
	private String cargoId;
	@ExportField(title="运单号",sort=20)
	private String waybillNumber;
	@ExportField(title="线下合同号",sort=30)
	private String contractNumber;
	@ExportField(title="客户订单号",sort=40)
	private String ClientNumber;
	@ExportField(title="发货地",sort=50)
	private String cargoFromName;
	@ExportField(title="收货地",sort=60)
	private String cargoToName;
	@ExportField(title="成交时间",sort=70,isTranf=1,tranfType=2)
	private Date dealTime;
	
	@ExportField(title="运输完成时间",sort=71,isTranf=1,tranfType=2)
	private Date applyReceiveTime;

	@ExportField(title="货品名称",sort=80)
	private String cargoName;
	@ExportField(title="重量(吨)",sort=90)
	private Double weight;
	@ExportField(title="货主名称",sort=100)
	private String organizationName;
	@ExportField(title="运单状态",sort=120,isTranf=0,dictType="waybillstate",tranfType=1)
	private String wayBillState;
	@ExportField(title="运输管理费用",sort=130)
	private BigDecimal managementFee;
	@ExportField(title="保险费",sort=140)
	private BigDecimal policyFee;
	@ExportField(title="运费垫付利息",sort=150)
	private BigDecimal accrual;
	@ExportField(title="是否开发票",sort=160,isTranf=1,tranfType=3,dictType="EYN")
	private String isReceipt;
	@ExportField(title="确认收货支付方式",sort=170,dictType="payType",isTranf=0,tranfType=1)
	private String confirmPayType;
	@ExportField(title="签收时间",sort=180,isTranf=1,tranfType=2)
	private Date happenTime;
	@ExportField(title="备注/未成交原因",sort=190)
	private String note;
	@ExportField(title="预付运费金额",sort=200)
	private BigDecimal prepayment;
	@ExportField(title="预付支付类型",sort=210,dictType="payType",isTranf=0,tranfType=1)
	private String prepaymentType;
	@ExportField(title="预付运费备注",sort=220)
	private String prepaymentNote;
	@ExportField(title="调整车价金额",sort=230)
	private BigDecimal adjustmentFee;
	@ExportField(title="总费用",sort=240)
	private BigDecimal allAmount;
	@ExportField(title="结算车价",sort=250)
	private BigDecimal jiesuanMoney;
	@ExportField(title="开票金额",sort=260)
	private BigDecimal paybillMoney;
	@ExportField(title="利润率",sort=270)
	private BigDecimal profit;
	@ExportField(title="发票票号",sort=280)
	private String receiptNO;
	@ExportField(title="开票主体",sort=290)
	private String paybillOrgan;
	@ExportField(title="结算状态标记",sort=300,dictType="balance_bj",isTranf=0,tranfType=1)
	private String state;
	@ExportField(title="对账单编号",sort=310)
	private String billNO;
	@ExportField(title="成本开票状态",sort=300,dictType="cost_status",isTranf=0,tranfType=1)
	private String costStatus;
	@ExportField(title="回单标记",sort=320,dictType="cost_bj",isTranf=0,tranfType=1)
	private String ishd;
	@ExportField(title="结算车价1",isShow=0)
	private BigDecimal amount;
	@ExportField(title="运费垫付",isShow=0)
	private BigDecimal carriage;
	@ExportField(title="利息",isShow=0)
	private BigDecimal accrualMoney;
	@ExportField(title="开始计息时间",isShow=0)
	private Date accrualStart;
	@ExportField(title="结束计息时间",isShow=0)
	private Date accrualEnd;
	@ExportField(title="利息类型",isShow=0)
	private int accrualType;
	@ExportField(title="利息费率",isShow=0)
	private BigDecimal accrualRate;
	@ExportField(title="其他费用",isShow=0)
	private BigDecimal otherFee;
	@ExportField(title="预付运费利息",isShow=0)
	private BigDecimal preAccrualMoney;
	@ExportField(title="预付运费时间",isShow=0)
	private Date prepaymentTime;
	@ExportField(title="代开发票",isShow=0)
	private BigDecimal daikaifapiao;
	@ExportField(title="发票费率",isShow=0)
	private BigDecimal invoiceRate;
	@ExportField(title="真实价格",isShow=0)
	private BigDecimal realCarrige;
	@ExportField(title="报价结算账户",sort=330)
	private String vehicleName;
	@ExportField(title = "后付油费", sort=245)
	private BigDecimal mediaOilPayAmount;
	
	
	public String getVehicleName() {
		return vehicleName;
	}

	public void setVehicleName(String vehicleName) {
		this.vehicleName = vehicleName;
	}

	public BigDecimal getInvoiceRate() {
		return invoiceRate;
	}

	public void setInvoiceRate(BigDecimal invoiceRate) {
		this.invoiceRate = invoiceRate;
	}
	public Date getApplyReceiveTime() {
		return applyReceiveTime;
	}

	public void setApplyReceiveTime(Date applyReceiveTime) {
		this.applyReceiveTime = applyReceiveTime;
	}
	public BigDecimal getDaikaifapiao() {
		 if (invoiceRate == null) {
			 daikaifapiao= BigDecimal.ZERO;
			 return daikaifapiao;
	      }
	      if (invoiceRate.equals(BigDecimal.ZERO)) {
	        	daikaifapiao= BigDecimal.ZERO;
	        	 return daikaifapiao;
	      }
	      if (null==managementFee) {
	    	  managementFee= BigDecimal.ZERO;
	      }
	      if (null==policyFee) {
	    	  policyFee= BigDecimal.ZERO;
	      }
	      if (null==accrualMoney) {
	    	  accrualMoney= BigDecimal.ZERO;
	      }
	      if (null==preAccrualMoney) {
	    	  preAccrualMoney= BigDecimal.ZERO;
	      }
	      if(null==otherFee){
	    	  otherFee = BigDecimal.ZERO;
	      }
	        // 发票费用=（成交车价+运输管理服务费+保险费+利息 + 其他费用）/（1-后台录入的发票费率） - （成交车价+运输管理服务费+保险费+利息 + 其他费用）
	        // （成交车价+运输管理服务费+保险费+利息 + 其他费用）
	        BigDecimal s1 = realCarrige.add(getManagementFee()).add(getPolicyFee()).add(getAccrualMoney()).add(getPreAccrualMoney()).add(getOtherFee());
	        // （1-后台录入的发票费率）
	        BigDecimal s2 = BigDecimal.ONE.subtract(invoiceRate);

	        daikaifapiao= s1.divide(s2, 4).subtract(s1);

	        return Round(daikaifapiao);
	}

	public void setDaikaifapiao(BigDecimal daikaifapiao) {
		
		this.daikaifapiao = daikaifapiao;
	}

	public Date getPrepaymentTime() {
		return prepaymentTime;
	}

	public void setPrepaymentTime(Date prepaymentTime) {
		this.prepaymentTime = prepaymentTime;
	}

	public BigDecimal getOtherFee() {
		return otherFee;
	}

	public void setOtherFee(BigDecimal otherFee) {
		this.otherFee = otherFee;
	}

	public BigDecimal getPreAccrualMoney() {
		  Date start = prepaymentTime;
	        if (start == null) {
	        	preAccrualMoney= BigDecimal.ZERO;
	        	return preAccrualMoney;
	        }

	        Date end = accrualEnd;
	        if (end == null) {
	            end = new Date();
	        }

	        //除以1000是为了转换成秒
	        if (accrualType == 1) {
	            long between = (end.getTime() - start.getTime()) / 1000;
	            long day = between / (24 * 3600);
	            if (accrualRate.compareTo(BigDecimal.ZERO) == 0) {
	            	preAccrualMoney = BigDecimal.ZERO;
	            } else {
	            	 BigDecimal sCarriage = null;
	                // 预付运费后的货款
	            	if(getPrepaymentType()!=null &&!getPrepaymentType().equals("物流管家垫付")){//物流管家算利息
	            		sCarriage=BigDecimal.ZERO;
	            	}else{
	            		sCarriage=getPrepayment();
	            	}
	                preAccrualMoney = sCarriage.multiply(accrualRate.divide(BigDecimal.valueOf(100)));
	            }

	            BigDecimal bDay = BigDecimal.valueOf(day);
	            preAccrualMoney = preAccrualMoney.multiply(bDay);
	        } else {
	            Calendar sCalendar = Calendar.getInstance();
	            if(this.accrualStart == null)
	            {
	                this.accrualStart = new Date();
	            }

	            sCalendar.setTime(this.accrualStart);

	            Calendar eCalendar = Calendar.getInstance();
	            eCalendar.setTime(end);

	            int sMonth = sCalendar.get(Calendar.MONTH);
	            int eMonth = eCalendar.get(Calendar.MONTH) + 1;
	            int month;
	            if (eMonth < sMonth) { // 不同年
	                month = eMonth - sMonth + 11;
	            } else {
	                month = eMonth - sMonth;
	            }

	            if (accrualRate.compareTo(BigDecimal.ZERO) == 0) {
	            	preAccrualMoney = BigDecimal.ZERO;
	            } else {
	            	 BigDecimal sCarriage = null;
		                // 预付运费后的货款
		            	if(getPrepaymentType()!=null && !getPrepaymentType().equals("物流管家垫付")){
		            		sCarriage=BigDecimal.ZERO;
		            	}else{
		            		sCarriage=getPrepayment();
		            	}
	                preAccrualMoney = sCarriage.multiply(accrualRate.divide(BigDecimal.valueOf(100)));
	            }

	            BigDecimal bMonth = BigDecimal.valueOf(month);
	            preAccrualMoney = preAccrualMoney.multiply(bMonth);
	        }
		return Round(preAccrualMoney);
	}

	public void setPreAccrualMoney(BigDecimal preAccrualMoney) {
		 this.preAccrualMoney=preAccrualMoney;
	}

	public void setAccrualMoney(BigDecimal accrualMoney){
	     
		 this.accrualMoney=accrualMoney;
	}
	public BigDecimal getAccrualMoney() {
		
		   Date start = accrualStart;
	        if (start == null) {
	        	accrualMoney= BigDecimal.ZERO;
	        	return accrualMoney;
	        }

	        Date end = accrualEnd;
	        if (end == null) {
	            end = new Date();
	        }

	        //除以1000是为了转换成秒
	        if (accrualType == 1) {
	            long between = (end.getTime() - start.getTime()) / 1000;
	            long day = between / (24 * 3600);
	            if (accrualRate.compareTo(BigDecimal.ZERO) == 0) {
	            	accrualMoney = BigDecimal.ZERO;
	            } else {
	                // 预付运费后的货款
	                BigDecimal sCarriage;
	                if (this.getPrepayment() == null) {
	                    sCarriage = carriage;
	                } else {
	                	if(getPrepaymentType()!=null && !getPrepaymentType().equals("物流管家垫付")){
		                    sCarriage = carriage.subtract(BigDecimal.ZERO);
	                	}else{
		                    sCarriage = carriage.subtract(this.getPrepayment());
	                	}
	                }
	                accrualMoney = sCarriage.multiply(accrualRate.divide(BigDecimal.valueOf(100)));
	            }

	            BigDecimal bDay = BigDecimal.valueOf(day);
	            accrualMoney = accrualMoney.multiply(bDay);
	        } else {
	            Calendar sCalendar = Calendar.getInstance();
	            sCalendar.setTime(start);

	            Calendar eCalendar = Calendar.getInstance();
	            eCalendar.setTime(end);

	            int sMonth = sCalendar.get(Calendar.MONTH);
	            int eMonth = eCalendar.get(Calendar.MONTH) + 1;
	            int month;
	            if (eMonth < sMonth) { // 不同年
	                month = eMonth - sMonth + 11;
	            } else {
	                month = eMonth - sMonth;
	            }

	            if (accrualRate.compareTo(BigDecimal.ZERO) == 0) {
	            	accrualMoney = BigDecimal.ZERO;
	            } else {
	            	BigDecimal sCarriage ;
	            	if(getPrepaymentType()!=null && !getPrepaymentType().equals("物流管家垫付")){
	                    sCarriage = carriage.subtract(BigDecimal.ZERO);
                	}else{
	                    sCarriage = carriage.subtract(this.getPrepayment());
                	}
	                // 预付运费后的货款
	                accrualMoney = sCarriage.multiply(accrualRate.divide(BigDecimal.valueOf(100)));
	            }

	            BigDecimal bMonth = BigDecimal.valueOf(month);
	            accrualMoney = accrualMoney.multiply(bMonth);
	        }
		
	       return Round(accrualMoney);
	 }
	public BigDecimal getCarriage() {
		return Round(carriage);
	}
	public void setCarriage(BigDecimal carriage) {
		this.carriage = carriage;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getCargoId() {
		return cargoId;
	}

	public void setCargoId(String cargoId) {
		this.cargoId = cargoId;
	}

	public String getWaybillNumber() {
		return waybillNumber;
	}

	public void setWaybillNumber(String waybillNumber) {
		this.waybillNumber = waybillNumber;
	}

	public String getContractNumber() {
		return contractNumber;
	}

	public void setContractNumber(String contractNumber) {
		this.contractNumber = contractNumber;
	}

	public String getClientNumber() {
		return ClientNumber;
	}

	public void setClientNumber(String clientNumber) {
		ClientNumber = clientNumber;
	}

	public String getCargoFromName() {
		return cargoFromName;
	}

	public void setCargoFromName(String cargoFromName) {
		this.cargoFromName = cargoFromName;
	}

	public String getCargoToName() {
		return cargoToName;
	}

	public void setCargoToName(String cargoToName) {
		this.cargoToName = cargoToName;
	}

	public Date getDealTime() {
		return dealTime;
	}

	public void setDealTime(Date dealTime) {
		this.dealTime = dealTime;
	}

	public String getCargoName() {
		return cargoName;
	}

	public void setCargoName(String cargoName) {
		this.cargoName = cargoName;
	}

	public Double getWeight() {
		return weight;
	}

	public void setWeight(Double weight) {
		this.weight = weight;
	}

	public String getOrganizationName() {
		return organizationName;
	}

	public void setOrganizationName(String organizationName) {
		this.organizationName = organizationName;
	}

	public String getWayBillState() {
		return wayBillState;
	}

	public void setWayBillState(String wayBillState) {
		this.wayBillState = wayBillState;
	}

	public BigDecimal getManagementFee() {
		return Round(managementFee);
	}

	public void setManagementFee(BigDecimal managementFee) {
		this.managementFee = managementFee;
	}

	public BigDecimal getPolicyFee() {
		return Round(policyFee);
	}

	public void setPolicyFee(BigDecimal policyFee) {
		this.policyFee = policyFee;
	}

	public BigDecimal getAccrual() {
		return getAccrualMoney().add(getPreAccrualMoney());
	}

	public void setAccrual(BigDecimal accrual) {
		this.accrual = accrual;
	}

	public String getIsReceipt() {
		return isReceipt;
	}

	public void setIsReceipt(String isReceipt) {
		this.isReceipt = isReceipt;
	}

	public String getConfirmPayType() {
		return confirmPayType;
	}

	public void setConfirmPayType(String confirmPayType) {
		this.confirmPayType = confirmPayType;
	}

	public Date getHappenTime() {
		return happenTime;
	}

	public void setHappenTime(Date happenTime) {
		this.happenTime = happenTime;
	}

	public String getNote() {
		
		return note=note.replaceAll("\\t","\t").replaceAll("\\n", "\n").replaceAll("\\r","\r");
	}

	public void setNote(String note) {
		this.note = note;
	}

	public BigDecimal getPrepayment() {
		return prepayment;
	}

	public void setPrepayment(BigDecimal prepayment) {
		this.prepayment = prepayment;
	}

	public String getPrepaymentType() {
		return prepaymentType;
	}

	public void setPrepaymentType(String prepaymentType) {
		this.prepaymentType = prepaymentType;
	}

	public String getPrepaymentNote() {
		if(null!=prepaymentNote && !prepaymentNote.equals("")){
			prepaymentNote=prepaymentNote.replaceAll("\\t","\t").replaceAll("\\n", "\n").replaceAll("\\r","\r");
		}
		return prepaymentNote;
	}

	public void setPrepaymentNote(String prepaymentNote) {
		this.prepaymentNote = prepaymentNote;
	}

	public BigDecimal getAdjustmentFee() {
		return adjustmentFee;
	}

	public void setAdjustmentFee(BigDecimal adjustmentFee) {
		this.adjustmentFee = adjustmentFee;
	}

	public BigDecimal getAllAmount() {
				//1运输管理费
				BigDecimal t1 = getManagementFee();
				if(null==t1){
					t1=BigDecimal.ZERO;
				}
				//2运费垫付
				BigDecimal t2 = getCarriage();
				if(null==t2){
					t2=BigDecimal.ZERO;
				}
				//3利息
				BigDecimal t3 = getAccrualMoney();
				if(null==t3){
					t3=BigDecimal.ZERO;
				}
				//4保险费
				BigDecimal t4 = getPolicyFee();
				if(null==t4){
					t4=BigDecimal.ZERO;
				}
				//5其他费用
				BigDecimal t5 = getOtherFee();
				if(null==t5){
					t5=BigDecimal.ZERO;
				}
				//6预付运费利息
				BigDecimal t6 = getPreAccrualMoney();
				if(null==t6){
					t6=BigDecimal.ZERO;
				}
				//7代开发票
				BigDecimal t7 = getDaikaifapiao();
				if(null==t7){
					t7=BigDecimal.ZERO;
				}
				allAmount = t1.add(t2).add(t3).add(t4).add(t5).add(t6).add(t7);
		return allAmount.setScale(2, BigDecimal.ROUND_HALF_UP);
	}

	public void setAllAmount(BigDecimal allAmount) {
		this.allAmount = allAmount;
	}

	public Date getAccrualStart() {
		return accrualStart;
	}

	public void setAccrualStart(Date accrualStart) {
		this.accrualStart = accrualStart;
	}

	public BigDecimal getRealCarrige() {
		return realCarrige;
	}

	public void setRealCarrige(BigDecimal realCarrige) {
		this.realCarrige = realCarrige;
	}

	public Date getAccrualEnd() {
		return accrualEnd;
	}

	public void setAccrualEnd(Date accrualEnd) {
		this.accrualEnd = accrualEnd;
	}

	public int getAccrualType() {
		return accrualType;
	}

	public void setAccrualType(int accrualType) {
		this.accrualType = accrualType;
	}

	public BigDecimal getAccrualRate() {
		return accrualRate;
	}

	public void setAccrualRate(BigDecimal accrualRate) {
		this.accrualRate = accrualRate;
	}

	public BigDecimal getJiesuanMoney() {
		if(null==adjustmentFee){
			adjustmentFee=BigDecimal.ZERO;
		}
		if(null == amount){
			amount=BigDecimal.ZERO;
		}
		return adjustmentFee.add(amount).setScale(2, BigDecimal.ROUND_HALF_UP);
	}

	public void setJiesuanMoney(BigDecimal jiesuanMoney) {
		this.jiesuanMoney = jiesuanMoney;
	}

	public BigDecimal getPaybillMoney() {
		return paybillMoney;
	}

	public void setPaybillMoney(BigDecimal paybillMoney) {
		this.paybillMoney = paybillMoney;
	}

	public BigDecimal getProfit() {
		return profit;
	}

	public void setProfit(BigDecimal profit) {
		this.profit = profit;
	}

	public String getReceiptNO() {
		return receiptNO;
	}

	public void setReceiptNO(String receiptNO) {
		this.receiptNO = receiptNO;
	}

	public String getPaybillOrgan() {
		return paybillOrgan;
	}

	public void setPaybillOrgan(String paybillOrgan) {
		this.paybillOrgan = paybillOrgan;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getBillNO() {
		return billNO;
	}

	public void setBillNO(String billNO) {
		this.billNO = billNO;
	}

	public String getIshd() {
		return ishd;
	}

	public void setIshd(String ishd) {
		this.ishd = ishd;
	}

	public BigDecimal getAmount() {
		return amount;
	}

	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}
	private BigDecimal Round(BigDecimal t) {
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        String roundT = decimalFormat.format(t);
        BigDecimal result = new BigDecimal(roundT);
        return result;
    }

	public String getCostStatus() {
		return costStatus;
	}

	public void setCostStatus(String costStatus) {
		this.costStatus = costStatus;
	}

	public BigDecimal getMediaOilPayAmount() {
		return mediaOilPayAmount;
	}

	public void setMediaOilPayAmount(BigDecimal mediaOilPayAmount) {
		this.mediaOilPayAmount = mediaOilPayAmount;
	}
}
